import requests
from lxml import etree
import parsel
import csv


f = open('猫咪_ 1.csv', mode='a', encoding='utf-8', newline='')
csv_writer = csv.DictWriter(f, fieldnames=[
            '地区',
            '店名',
            '标题',
            '价格' ,
            '浏览次数',
            '在售只数',
            '年龄',
            '品种',
            '预防针',
            '联系人',
            '练习方式',
            '异地运费',
            '是否纯种',
            '猫咪性别',
            '驱虫情况',
            '能否视频',
            '详情页'
])
csv_writer.writeheader()    # 写入表头
for page in range(1, 20):
    print(f'——————————————————正在抓取低{page}页——————————————————')
    # url = 'http://www.maomijiaoyi.com/index.php?/chanpinliebiao_c_2.html'
    url = f'http://www.maomijiaoyi.com/index.php?/chanpinliebiao_c_2_{page}--24.html'
    heraers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36'
    }

    resp = requests.get(url=url, headers=heraers)
    # print(resp.text)

    # 数据解析
    et = etree.HTML(resp.text)
    comtent = et.xpath('//*[@id="content"]/div[3]/div/div')
    # print(comtent)
    for a in comtent:
        herfs = a.xpath('./a/@href')
        areas = a.xpath('./a/div[3]/div[2]/span[2]/text()')
        areas = [i.strip() for i in areas]        # 列表推导式，  strip() 去除空白
        # print(herfs)
        # print(areas)
        for index in zip(herfs, areas):
            index_url = 'http://www.maomijiaoyi.com' + index[0]     # 详情页
            ares = index[1]     # 地区

            # 获取详情页数据
            resp_1 = requests.get(url=index_url, headers=heraers)
            selector = parsel.Selector(resp_1.text)
            # 标题
            title =selector.css('.detail_text .title::text').get().strip() # 取一个       getall 取多个
            # 店名
            shop = selector.css('.dinming::text').get().strip()
            price = selector.xpath('//*[@id="content"]/div[3]/div[1]/div[2]/div[2]/div[1]/span[2]/text()').get()    # 价格
            views = selector.xpath('//*[@id="content"]/div[3]/div[1]/div[2]/div[2]/div[1]/span[4]/text()').get()


            num = selector.xpath('//*[@id="content"]/div[3]/div[1]/div[2]/div[3]/div[1]/div[2]/text()').get()  # 在售只数
            # 年龄
            age = selector.css('#content > div.center_1220.detail > div.left.top > div.detail_text > div.info2 > div:nth-child(2) > div.red::text').get()
            kind = selector.xpath('//*[@id="content"]/div[3]/div[1]/div[3]/div[2]/div[1]/div[2]/div[2]/span/text()').get() # 品种
            prevention = selector.xpath('//*[@id="content"]/div[3]/div[1]/div[2]/div[3]/div[4]/div[2]/text()').get()   # 预防针
            person = selector.xpath('//*[@id="content"]/div[3]/div[1]/div[2]/div[4]/div[1]/div[2]/text()').get()   # 联系人
            phone = selector.xpath('//*[@id="content"]/div[3]/div[1]/div[2]/div[4]/div[2]/div[2]/text()').get()    # 练习方式
            postage = selector.xpath('//*[@id="content"]/div[3]/div[1]/div[2]/div[4]/div[3]/div[2]/text()').get().strip() # 异地运费
            purebred = selector.xpath('//*[@id="content"]/div[3]/div[1]/div[3]/div[2]/div[1]/div[2]/div[1]/span/text()').get().strip() # 是否纯种
            sex = selector.xpath('//*[@id="content"]/div[3]/div[1]/div[3]/div[2]/div[1]/div[2]/div[4]/span/text()').get().strip()  # 猫咪性别
            worming = selector.xpath('//*[@id="content"]/div[3]/div[1]/div[3]/div[2]/div[2]/div[2]/div[2]/span/text()').get().strip()   # 驱虫情况
            video = selector.xpath('//*[@id="content"]/div[3]/div[1]/div[3]/div[2]/div[2]/div[2]/div[4]/span/text()').get().strip()  # 能否视频

            dic = {
                '地区': ares,
                '店名': shop,
                '标题': title,
                '价格' : price,
                '浏览次数': views,
                '在售只数': num,
                '年龄': age,
                '品种': kind,
                '预防针': prevention,
                '联系人': person,
                '练习方式': phone,
                '异地运费': postage,
                '是否纯种': purebred,
                '猫咪性别': sex,
                '驱虫情况': worming,
                '能否视频': video,
                '详情页': index_url
            }
            csv_writer.writerow(dic)
            print(title, ares, price, kind, prevention, person, phone, postage, purebred, sex, worming, video, index_url)

# selector = parsel.Selector(resp.text)
# selector.css(  )
